﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TextEditor.TextDocument.Control
{
    public partial class frmTableBorderAndBcolor : DevComponents.DotNetBar.Office2007Form
    {
        public frmTableBorderAndBcolor()
        {
            InitializeComponent();
            this.cboLineDashStyle.SelectedIndex = 0;
            this.cbLSlash.Selected = false;
            this.cbRSlash.Selected = false;
            this.comboBox1.SelectedIndex = 0;
        }

        string borderStyle = string.Empty;
        public string Style
        {
            get
            {
                return borderStyle;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            using (Pen p = new Pen(Color.LightGray))
            {
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;
                e.Graphics.DrawRectangle(p, 260, 90, 65, 65);
                p.DashStyle = (System.Drawing.Drawing2D.DashStyle)ControlsUnity.GetNameToEnum(((DevComponents.Editors.ComboItem)this.cboLineDashStyle.SelectedItem).Tag.ToString(), typeof(System.Drawing.Drawing2D.DashStyle));
                p.Color = colorDialog1.Color;
                if (this.cbTop.Selected)
                {
                    e.Graphics.DrawLine(p, 260, 90, 325, 90);
                }
                if (this.cbLeft.Selected)
                {
                    e.Graphics.DrawLine(p, 260, 90, 260, 155);
                }
                if (this.cbRight.Selected)
                {
                    e.Graphics.DrawLine(p, 325, 90, 325, 155);
                }
                if (this.cbRight.Selected)
                {
                    e.Graphics.DrawLine(p, 325, 90, 325, 155);
                }
                if (this.cbBottom.Selected)
                {
                    e.Graphics.DrawLine(p, 260, 155, 325, 155);
                }
                if (this.cbLSlash.Selected)
                {
                    e.Graphics.DrawLine(p, 260, 90, 325, 155);
                }

                if (this.cbRSlash.Selected)
                {
                    e.Graphics.DrawLine(p, 325, 90, 260, 155);
                }
            }
            base.OnPaint(e);
        }

        public int SelectedIndex
        {
            get 
            {
                return this.comboBox1.SelectedIndex;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = label7.BackColor;
            if (colorDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                label7.BackColor = colorDialog1.Color;
                Refresh();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string style = "border:";
            if (cbLeft.Selected)
                style += "l";
            if (cbRight.Selected)
                style += "r";
            if (cbTop.Selected)
                style += "t";
            if (cbBottom.Selected)
                style += "b";
            style += ";borderSlash:";
            if (cbLSlash.Selected)
                style += "n";
            if (cbRSlash.Selected)
                style += "m";
            style += ";linear:" + this.cboLineDashStyle.Tag;
            //+";";
            //style += "borderColor:" + ZYCommon.StringCommon.ColorToHtml((Color)this.colorDialog1.Color);
            borderStyle = style;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        public void SetStyle(string styleStr)
        {
            if (string.IsNullOrEmpty(styleStr))
                return;
            string[] styles = styleStr.Split(';');
            foreach (string str in styles)
            {
                string[] str2 = str.Split(':');
                if (str2.Length > 1)
                {
                    if (string.IsNullOrEmpty(str2[1]))
                        continue;
                    if (str2[0] == "border")
                    {
                        foreach (char item in str2[1])
                        {
                            switch (item)
                            {
                                case 'l':
                                    this.cbLeft.Selected = true;
                                    break;
                                case 'r':
                                    this.cbRight.Selected = true;
                                    break;
                                case 't':
                                    this.cbTop.Selected = true;
                                    break;
                                case 'b':
                                    this.cbBottom.Selected = true;
                                    break;
                            }
                        }
                    }

                    if (str2[0] == "borderSlash")
                    {
                        foreach (char ch2 in str2[1])
                        {
                            if (ch2 == 'n')
                                this.cbLSlash.Selected = true;

                            if (ch2 == 'm')
                                this.cbRSlash.Selected = true;

                        }
                    }

                    if (str2[0] == "linear")
                    {
                        this.cboLineDashStyle.SetDashStyle(str2[1]);
                    }

                    if (str2[0] == "borderColor")
                    {
                        this.colorDialog1.Color = ZYCommon.StringCommon.ColorFromHtml(str2[1], Color.Black);
                        this.label7.BackColor = this.colorDialog1.Color;
                    }
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.No;
            this.Close();
        }
    }
}
